package com.sophialex.health.record.dialog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.sophia.common.pojo.SophiaCommonPojo;
import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.common.platform.util.CustomGenerator;
import com.sophialex.health.person.vo.HrIssue;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.util.DataSource;
import com.sophialex.health.record.common.util.ListenerUtil;
import com.sophialex.health.record.common.utils.CommonUtils;
import com.sophialex.health.record.common.utils.ImageUtil;
import com.sophialex.health.system.frame.dialog.DialogManager;
import com.sophialex.health.system.frame.util.LayoutUtil;

/**
 * 角色设置窗口
 * <p>
 * Title:Health
 * </p>
 * <p>
 * RoleDialog
 * </p>
 * Alex Apr 5, 2011 5:14:45 PM create
 * 
 * @author <a href='wanghao924@126.com'>Alex</a> QQ: 873987060;
 * @version 1.0
 */
public class HrIssueDialog extends Dialog {
	protected Object result;

	protected Shell shell;

	private Composite composite_2;

	private Composite composite_1;

	private Button btnc;

	private Button btns;

	HrIssue hrIssue;

	List<HrIssue> hrIssueList = new ArrayList<HrIssue>();

	private FormData formData_2 = new FormData();

	private Label lable_jkwt;

	private FormData fd_lable_jkwt;

	private Label labl_wtlx;

	private FormData fd_labl_wtlx;

	private Combo combo_jkwt;

	private Combo combo_wtlx;

	private Label lable_icd10;

	private Text text_icd10;

	private Label label_fxsj;

	private DateTime text_finddate;


//	private Label label_qzjg;

	private Text text_jzjg;

	private Label label_sfzy;

	private Combo combo_sfzy;

	private Label label_zysj;

	private DateTime text_zysj;
	
	private Label label_chuli;
    
	private Combo combo_chuli;

	private FormData fd_text_jzjg;
	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public HrIssueDialog(Shell parent, int style) {
		super(parent, style);
		setText(Messages.getString("HrIssueAction.0"));
	}
	
	public HrIssueDialog(HrIssue hrIssue,Shell parent, int style) {
		super(parent, style);
		this.hrIssue = hrIssue;
		setText(Messages.getString("HrIssueAction.1"));
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		Display display = getParent().getDisplay();
		LayoutUtil.centerShell(display, shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return hrIssue;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(522, 395);
		shell.setText(getText());
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			Composite composite = new Composite(shell, SWT.NONE);
			composite.setLayout(new FormLayout());

			{
				composite_2 = new Composite(composite, SWT.NONE);
				GridLayout gridLayout = new GridLayout(1, false);
				gridLayout.verticalSpacing = 10;
				composite_2.setLayout(gridLayout);
				{
					FormData formData_1;
					formData_1 = new FormData();
					formData_1.top = new FormAttachment(0);
					formData_1.right = new FormAttachment(0, 0);
					formData_1.bottom = new FormAttachment(100, -30);
					formData_1.left = new FormAttachment(0, 0);
					composite_2.setLayoutData(formData_1);
				}
				{
					composite_1 = new Composite(composite, SWT.NONE);

					formData_2.bottom = new FormAttachment(composite_1);
					composite_1.setLayout(null);
					{
						FormData formData = new FormData();
						formData.top = new FormAttachment(100, -50);
						formData.left = new FormAttachment(0);
						formData.right = new FormAttachment(100);
						formData.bottom = new FormAttachment(100);
						composite_1.setLayoutData(formData);
					}
					{
						Label label_1 = new Label(composite_1, SWT.SEPARATOR | SWT.HORIZONTAL);
						label_1.setBounds(0, 2, 516, 2);
					}
					{
						btnc = new Button(composite_1, SWT.NONE);
						btnc.setBounds(434, 10, 72, 22);
						btnc.setText(Messages.getString("HrIssueDialog.1"));
					}
					{
						btns = new Button(composite_1, SWT.NONE);
						btns.setBounds(356, 10, 72, 22);
						btns.setText(Messages.getString("HrIssueDialog.0"));
					}
				}
			}
			{
				lable_jkwt = new Label(composite, SWT.NONE);
				fd_lable_jkwt = new FormData();
				lable_jkwt.setLayoutData(fd_lable_jkwt);
				lable_jkwt.setText(Messages.getString("HrIssueDialog.2"));
			}
			{
				fd_lable_jkwt.right = new FormAttachment(100, -433);
			}
			{
				labl_wtlx = new Label(composite, SWT.NONE);
				fd_lable_jkwt.top = new FormAttachment(0, 42);
				fd_labl_wtlx = new FormData();
				fd_labl_wtlx.top = new FormAttachment(lable_jkwt, 0, SWT.TOP);
				labl_wtlx.setLayoutData(fd_labl_wtlx);
				labl_wtlx.setText(Messages.getString("HrIssueDialog.3"));
			}
			{
				fd_labl_wtlx.right = new FormAttachment(100, -196);
			}

			combo_wtlx = new Combo(composite, SWT.READ_ONLY);
			FormData fd_combo_wtlx = new FormData();
			fd_combo_wtlx.right = new FormAttachment(labl_wtlx, 156, SWT.RIGHT);
			fd_combo_wtlx.bottom = new FormAttachment(lable_jkwt, 0, SWT.BOTTOM);
			fd_combo_wtlx.left = new FormAttachment(labl_wtlx, 6);
			fd_combo_wtlx.top = new FormAttachment(0, 34);
			combo_wtlx.setLayoutData(fd_combo_wtlx);
			{
				combo_jkwt = new Combo(composite, SWT.READ_ONLY);
				FormData fd_combo_jkwt = new FormData();
				fd_combo_jkwt.right = new FormAttachment(lable_jkwt, 154, SWT.RIGHT);
				fd_combo_jkwt.bottom = new FormAttachment(lable_jkwt, 0, SWT.BOTTOM);
				fd_combo_jkwt.left = new FormAttachment(lable_jkwt, 4);
				combo_jkwt.setLayoutData(fd_combo_jkwt);
			}
			{
				lable_icd10 = new Label(composite, SWT.NONE);
				FormData fd_lable_icd10 = new FormData();
				fd_lable_icd10.top = new FormAttachment(lable_jkwt, 43);
				fd_lable_icd10.right = new FormAttachment(lable_jkwt, 0, SWT.RIGHT);
				lable_icd10.setLayoutData(fd_lable_icd10);
				lable_icd10.setText(Messages.getString("HrIssueDialog.4"));
			}
			{
				text_icd10 = new Text(composite, SWT.BORDER);
				FormData fd_text_icd10 = new FormData();
				fd_text_icd10.right = new FormAttachment(combo_jkwt, 0, SWT.RIGHT);
				fd_text_icd10.top = new FormAttachment(combo_jkwt, 35);
				fd_text_icd10.bottom = new FormAttachment(lable_icd10, 0, SWT.BOTTOM);
				fd_text_icd10.left = new FormAttachment(lable_icd10, 4);
				text_icd10.setLayoutData(fd_text_icd10);
			}

			{
				label_fxsj = new Label(composite, SWT.NONE);
				FormData fd_label_fxsj = new FormData();
				fd_label_fxsj.bottom = new FormAttachment(lable_icd10, 0, SWT.BOTTOM);
				fd_label_fxsj.right = new FormAttachment(labl_wtlx, 0, SWT.RIGHT);
				label_fxsj.setLayoutData(fd_label_fxsj);
				label_fxsj.setText(Messages.getString("HrIssueDialog.6"));
			}
			{
				text_finddate = new DateTime(composite, SWT.BORDER | SWT.LONG | SWT.DROP_DOWN);
				FormData fd_text_finddate = new FormData();
				fd_text_finddate.bottom = new FormAttachment(composite_1, -204);
				fd_text_finddate.top = new FormAttachment(combo_wtlx, 35);
				fd_text_finddate.right = new FormAttachment(combo_wtlx, 0, SWT.RIGHT);
				fd_text_finddate.left = new FormAttachment(label_fxsj, 6);
				text_finddate.setLayoutData(fd_text_finddate);
			}

			{
				label_chuli = new Label(composite, SWT.NONE);
				FormData fd_chuli = new FormData();
				fd_chuli.top = new FormAttachment(lable_icd10, 43);
				fd_chuli.right = new FormAttachment(lable_jkwt, 0, SWT.RIGHT);
				label_chuli.setLayoutData(fd_chuli);
				label_chuli.setText(Messages.getString("HrIssueDialog.9"));
			}
			{
				combo_chuli = new Combo(composite, SWT.READ_ONLY);
				FormData fd_combo_chuli = new FormData();
				fd_combo_chuli.left = new FormAttachment(label_chuli, 4);
				fd_combo_chuli.right = new FormAttachment(100, -279);
				fd_combo_chuli.bottom = new FormAttachment(label_chuli, 0, SWT.BOTTOM);
				combo_chuli.setLayoutData(fd_combo_chuli);
			}
//			{
//				label_qzjg = new Label(composite, SWT.NONE);
//				FormData fd_label_qzjg = new FormData();
//				fd_label_qzjg.top = new FormAttachment(lable_icd10, 43);
//				fd_label_qzjg.right = new FormAttachment(lable_jkwt, 0, SWT.RIGHT);
//				label_qzjg.setLayoutData(fd_label_qzjg);
//				label_qzjg.setText(Messages.getString("HrIssueDialog.5"));
//			}
//			{
//				text_jzjg = new Text(composite, SWT.BORDER);
//				fd_text_jzjg = new FormData();
//				fd_text_jzjg.right = new FormAttachment(combo_jkwt, 0, SWT.RIGHT);
//				fd_text_jzjg.bottom = new FormAttachment(label_qzjg, 0, SWT.BOTTOM);
//				fd_text_jzjg.left = new FormAttachment(combo_jkwt, 0, SWT.LEFT);
//				text_jzjg.setLayoutData(fd_text_jzjg);
//			}
			{
				label_sfzy = new Label(composite, SWT.NONE);
				FormData fd_label_sfzy = new FormData();
				fd_label_sfzy.bottom = new FormAttachment(label_chuli, 0, SWT.BOTTOM);
				fd_label_sfzy.right = new FormAttachment(labl_wtlx, 0, SWT.RIGHT);
				label_sfzy.setLayoutData(fd_label_sfzy);
				label_sfzy.setText(Messages.getString("HrIssueDialog.7"));
			}
			{
				combo_sfzy = new Combo(composite, SWT.READ_ONLY);
//				fd_text_jzjg.top = new FormAttachment(combo_sfzy, 0, SWT.TOP);
				FormData fd_text_sfzy = new FormData();
				fd_text_sfzy.bottom = new FormAttachment(label_sfzy,0,SWT.BOTTOM);
				fd_text_sfzy.top = new FormAttachment(combo_chuli, 0,SWT.TOP);
				fd_text_sfzy.right = new FormAttachment(combo_wtlx, 0, SWT.RIGHT);
				fd_text_sfzy.left = new FormAttachment(label_sfzy, 6);
				combo_sfzy.setLayoutData(fd_text_sfzy);
			}

			{
				label_zysj = new Label(composite, SWT.NONE);
				FormData fd_label_zysj = new FormData();
				fd_label_zysj.top = new FormAttachment(label_chuli, 43);
				fd_label_zysj.right = new FormAttachment(label_chuli, 0, SWT.RIGHT);
				label_zysj.setLayoutData(fd_label_zysj);
				label_zysj.setText(Messages.getString("HrIssueDialog.8"));
			}
			{
				text_zysj = new DateTime(composite, SWT.BORDER | SWT.LONG | SWT.DROP_DOWN);
				FormData fd_text_zysj = new FormData();
				fd_text_zysj.right = new FormAttachment(combo_chuli, 0, SWT.RIGHT);
				fd_text_zysj.left = new FormAttachment(combo_chuli,0,SWT.LEFT);
				fd_text_zysj.bottom = new FormAttachment(label_zysj, 0, SWT.BOTTOM);
				text_zysj.setLayoutData(fd_text_zysj);
			}
			setControlColor(ImageUtil.bj, composite);
			setControlColorOwner(ImageUtil.COLOR_LIST_BACKGROUND, composite_1);
			
			
		}

		initCombo();
		setData();
		addlistener();
	}
    
	private void setData(){
		if(hrIssue != null){
			getCombo_jkwt().setText(hrIssue.getIssueName());
			getText_icd10().setText(hrIssue.getIcd10());
			getCombo_chuli().setText(hrIssue.getTratement()==null?"--请选择--":hrIssue.getTratement());
			CommonUtils.setComboValue(getCombo_wtlx(), "ISSUE_TYPE", hrIssue.getIssueType());
			CommonUtils.setComboValue(getCombo_sfzy(), "YES_NO", hrIssue.getIsCure());
		}
		if(CommonUtils.getComboText(getCombo_sfzy()).equals("是")){
			label_zysj.setVisible(true);
			text_zysj.setVisible(true);
		}else{
			label_zysj.setVisible(false);
			text_zysj.setVisible(false);
		}
	}
	
	private void initCombo() {
		CommonUtils.initCombo(getCombo_jkwt(), "ISSUE",false);
		CommonUtils.initCombo(getCombo_wtlx(), "ISSUE_TYPE",false);
		CommonUtils.initCombo(getCombo_sfzy(), "YES_NO", "否");
		CommonUtils.initCombo(getCombo_chuli(), "WTMLCL",true);
		
	}

	private void addlistener() {

		btnc.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		btns.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		getCombo_sfzy().addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(CommonUtils.getComboText(getCombo_sfzy()).equals("是")){
					label_zysj.setVisible(true);
					text_zysj.setVisible(true);
				}else{
					label_zysj.setVisible(false);
					text_zysj.setVisible(false);
				}
				
			}
		});
		for(int i = 0; i < composite_1.getChildren().length; i++) {
			composite_1.getChildren()[i].addTraverseListener(ListenerUtil.traverseListener);
		}
	}

	private boolean valdate() {
		boolean res = true;
		//		if(text.getText().equals("")) { //$NON-NLS-1$
		//			DialogManager.invokeWarnDlg(Messages.getString("RoleDialog.5")); //$NON-NLS-1$
		// res = false;
		// text.setFocus();
		// }
		return res;
	}

	protected void doSave() {

		boolean flagAdd = false;
		if(!valdate())
			return;
		if(hrIssue == null) {
			flagAdd = true;
			hrIssue = new HrIssue();
			hrIssue.setId(CustomGenerator.generator());
		}
		
		
		hrIssue.setPersonId(SuperContext.currentPerson.getId());
		hrIssue.setIssueName(getCombo_jkwt().getText());
		hrIssue.setIssueType(CommonUtils.getComboValue(getCombo_wtlx()));
		hrIssue.setIcd10(getText_icd10().getText());
		hrIssue.setIcd10Name(getText_icd10().getText());
		hrIssue.setHappenDate(CommonUtils.getDateFromDateTime(getText_finddate()));
		hrIssue.setCureDate(CommonUtils.getDateFromDateTime(getText_zysj()));
		hrIssue.setTratement(CommonUtils.getComboText(getCombo_chuli()));
		hrIssue.setIsCure(CommonUtils.getComboValue(getCombo_sfzy()));
		try {
			boolean returnFlag = false;
			if(flagAdd){
				hrIssue.setCommonPojo(CommonUtils.getCommonPojo());
				returnFlag = DataSource.hrIssueService.save(hrIssue);
			}else{
				SophiaCommonPojo common = CommonUtils.getCommonPojo();
				common.setUpdateTime(new Date());
				common.setOperatorId(1l);
				common.setChecksum("0");
				hrIssue.setCommonPojo(common);
				returnFlag = DataSource.hrIssueService.update(hrIssue);
			}
			
			if(returnFlag) {
				shell.close();
				DialogManager.invokeInfoDlg(Messages.getString("CommonInfo.0"));  
			} else {
				throw new Exception();
			}
		}
		catch(Exception e) {
			DialogManager.invokeInfoDlg(Messages.getString("CommonInfo.1"));  
			e.printStackTrace();
		}
	}

	private void setControlColor(Color color, Composite com) {
		com.setBackground(color);
		for(Control c : com.getChildren()) {
			if(c instanceof Text || c instanceof Combo)
				continue;
			c.setBackground(color);
		}
	}

	private void setControlColorOwner(Color color, Composite com) {
		com.setBackground(color);
	}

	public Combo getCombo_jkwt() {
		return combo_jkwt;
	}

	public Combo getCombo_wtlx() {
		return combo_wtlx;
	}

	public Combo getCombo_sfzy() {
		return combo_sfzy;
	}

	public Text getText_icd10() {
		return text_icd10;
	}

	public Text getText_jzjg() {
		return text_jzjg;
	}

	public DateTime getText_zysj() {
		return text_zysj;
	}

	public DateTime getText_finddate() {
		return text_finddate;
	}
	
	public Combo getCombo_chuli() {
		return combo_chuli;
	}

}
